*** MODELING
** Table 1
* Overall/Base Model

logit vote_2022p vote_2018p age_bounded_100 age_bounded_100_sq i.female income educ_prob_college married asian_fix black_nh_fix hisp_fix race_other_nh_fix ideology_continuous rsfrac_2022 rgfrac_2022 dsfrac_2022  dgfrac_2022 i.open_unaff_primary i.open_primary i.non_partisan_primary, cluster(state_identification)

margins female

margins female, pwcompare(effects) asobserved saving(overall, replace)

margins, dydx(female) saving(overall, replace)
marginsplot, recast(bar) ///
title("Overall")
graph export "gender margins overall.png", replace

estimates store overall_pto_all

* Republicans
logit vote_2022p vote_2018p age_bounded_100 age_bounded_100_sq i.female income educ_prob_college married asian_fix black_nh_fix hisp_fix race_other_nh_fix ideology_continuous rsfrac_2022 rgfrac_2022 i.open_unaff_primary i.open_primary i.non_partisan_primary if pid3==1, cluster(state_identification)

margins female
margins female, pwcompare(effect) asobserved
margins, dydx(female) saving(republican, replace)

marginsplot, recast(bar) ///
title("Republicans")
graph export "gender margins republican.png", replace

estimates store repub_pto_all

* Independents
logit vote_2022p vote_2018p age_bounded_100 age_bounded_100_sq i.female income educ_prob_college married asian_fix black_nh_fix hisp_fix race_other_nh_fix ideology_continuous rsfrac_2022 rgfrac_2022 dsfrac_2022 dgfrac_2022 i.open_unaff_primary i.open_primary i.non_partisan_primary if pid3==2, cluster(state_identification)

margins female
margins female, pwcompare(effect) asobserved
margins, dydx(female) saving(independent, replace)

marginsplot, recast(bar) ///
title("Independents")
graph export "gender margins independent.png", replace

estimates store indpt_pto_all

* Democrats
logit vote_2022p vote_2018p age_bounded_100 age_bounded_100_sq i.female income educ_prob_college married asian_fix black_nh_fix hisp_fix race_other_nh_fix ideology_continuous dsfrac_2022 dgfrac_2022 i.open_unaff_primary i.open_primary i.non_partisan_primary if pid3==3, cluster(state_identification)

margins female
margins female, pwcompare(effect) asobserved
margins, dydx(female) saving(democrat, replace)
marginsplot, recast(bar) ///
title("Democrats")
graph export "gender margins democrats.png", replace
estimates store dem_pto_all

*****Combomarginsplot for Table 1
ssc install combomarginsplot, replace

combomarginsplot overall republican independent democrat, ///
recast(bar) ///
file1opts(color(gs12)) ci1opts(color(black)) ///
labels("Overall" "Republicans" "Independents" "Democrats") ///
xtitle("") ytitle("Effect of Being a Woman on Pr(Turnout)") ///
ylab(-.03(.01)0.03, format(%9.2f)) ///
title("")

graph export "Effects of Gender in all 4 models.png", replace


*****Creating Tables******

esttab overall_pto_all  repub_pto_all  indpt_pto_all dem_pto_all using Overall_PTO_Models_gender.csv, replace se(3) label  ///
		b(%9.2f) star(* .1 ** .05 *** .01) nolz nobase nogaps s(N ll r2_p bic) ///
		varlabels(age_bounded_100 "Age" age_bounded_100_sq "Age Squared" 1.female "Female" asian_fix "Asian" black_nh_fix "Black" hisp_fix "Latino" ///
		race_other_nh_fix "Race Other" married "Married" educ_prob_college "Pr(Bachelor's degree)" income "Income" ideology_continuous ///
		"Catalist Ideology" _cons "Constant" 1.open_unaff_primary "Open-Unaffl. Voters Primary State" 1.open_primary "Open Primary State" ///
		1.non_partisan_primary "Non-partisan Primary State" ///
		rfrac_overall "Rep. Fract. 2022" dfrac_overall "Dem. Fract. 2022" ///
		vote_2018p "Vote in 2018 Primary") ///
		mtitles("Overall" "Republicans" ///
		"Independents" "Democrats" ) ///
		order(1.female vote_2018p age_bounded_100  age_bounded_100_sq ///
		asian_fix black_nh_fix hisp_fix race_other_nh_fix married educ_prob_college income ///
		ideology_continuous 1.open_unaff_primary 1.open_primary 1.non_partisan_primary)

		
***Robustness with women candidates data

hist percent_womancandidates

*** MODELING
** Woman Candidate Tables
* Overall/Base Model

logit vote_2022p vote_2018p age_bounded_100 age_bounded_100_sq i.female c.percent_womancandidates income educ_prob_college married asian_fix black_nh_fix hisp_fix race_other_nh_fix ideology_continuous rsfrac_2022 rgfrac_2022 dsfrac_2022  dgfrac_2022 i.open_unaff_primary i.open_primary i.non_partisan_primary, cluster(state_identification)

margins female

margins female, pwcompare(effects) asobserved saving(overall_percentwoman, replace)

margins, dydx(female) saving(overall_percentwomen, replace)
marginsplot, recast(bar) ///
title("Overall")

estimates store overall_percentwomen

logit vote_2022p vote_2018p age_bounded_100 age_bounded_100_sq i.female i.woman_in_primary income educ_prob_college married asian_fix black_nh_fix hisp_fix race_other_nh_fix ideology_continuous rsfrac_2022 rgfrac_2022 dsfrac_2022  dgfrac_2022 i.open_unaff_primary i.open_primary i.non_partisan_primary, cluster(state_identification)

margins female

margins female, pwcompare(effects) asobserved saving(overall_womancandummy, replace)

margins, dydx(female) saving(overall_womancandummy, replace)
marginsplot, recast(bar) ///
title("Overall")

estimates store overall_womancandummy

****Republicans
logit vote_2022p vote_2018p age_bounded_100 age_bounded_100_sq i.female c.percent_womancandidates income educ_prob_college married asian_fix black_nh_fix hisp_fix race_other_nh_fix ideology_continuous rsfrac_2022 rgfrac_2022 i.open_unaff_primary i.open_primary i.non_partisan_primary if pid3==1, cluster(state_identification)

margins female
margins female, pwcompare(effect) asobserved
margins, dydx(female) saving(repub_percentwomen, replace)

marginsplot, recast(bar) ///
title("Republicans")


estimates store repub_percentwomen

logit vote_2022p vote_2018p age_bounded_100 age_bounded_100_sq i.female i.woman_in_primary income educ_prob_college married asian_fix black_nh_fix hisp_fix race_other_nh_fix ideology_continuous rsfrac_2022 rgfrac_2022 i.open_unaff_primary i.open_primary i.non_partisan_primary if pid3==1, cluster(state_identification)

margins female
margins female, pwcompare(effect) asobserved
margins, dydx(female) saving(repub_womancandummy, replace)

marginsplot, recast(bar) ///
title("Republicans")


estimates store repub_womancandummy

****Independents
logit vote_2022p vote_2018p age_bounded_100 age_bounded_100_sq i.female c.percent_womancandidates income educ_prob_college married asian_fix black_nh_fix hisp_fix race_other_nh_fix ideology_continuous rsfrac_2022 rgfrac_2022 dsfrac_2022 dgfrac_2022 i.open_unaff_primary i.open_primary i.non_partisan_primary if pid3==2, cluster(state_identification)

margins female
margins female, pwcompare(effect) asobserved
margins, dydx(female) saving(indp_percentwomen, replace)

marginsplot, recast(bar) ///
title("Independents")


estimates store indp_percentwomen

logit vote_2022p vote_2018p age_bounded_100 age_bounded_100_sq i.female i.woman_in_primary income educ_prob_college married asian_fix black_nh_fix hisp_fix race_other_nh_fix ideology_continuous rsfrac_2022 rgfrac_2022 dsfrac_2022 dgfrac_2022 i.open_unaff_primary i.open_primary i.non_partisan_primary if pid3==2, cluster(state_identification)

margins female
margins female, pwcompare(effect) asobserved
margins, dydx(female) saving(indp_womancandummy, replace)

marginsplot, recast(bar) ///
title("Independents")


estimates store indp_womancandummy

****Democrats
logit vote_2022p vote_2018p age_bounded_100 age_bounded_100_sq i.female c.percent_womancandidates income educ_prob_college married asian_fix black_nh_fix hisp_fix race_other_nh_fix ideology_continuous dsfrac_2022 dgfrac_2022 i.open_unaff_primary i.open_primary i.non_partisan_primary if pid3==3, cluster(state_identification)

margins female
margins female, pwcompare(effect) asobserved
margins, dydx(female) saving(dem_percentwomen, replace)

marginsplot, recast(bar) ///
title("Democrats")


estimates store dem_percentwomen

logit vote_2022p vote_2018p age_bounded_100 age_bounded_100_sq i.female i.woman_in_primary income educ_prob_college married asian_fix black_nh_fix hisp_fix race_other_nh_fix ideology_continuous dsfrac_2022 dgfrac_2022 i.open_unaff_primary i.open_primary i.non_partisan_primary if pid3==3, cluster(state_identification)

margins female
margins female, pwcompare(effect) asobserved
margins, dydx(female) saving(dem_womancandummy, replace)

marginsplot, recast(bar) ///
title("Democrats")


estimates store dem_womancandummy


ssc install combomarginsplot, replace

combomarginsplot overall_percentwomen repub_percentwomen indp_percentwomen dem_percentwomen, ///
recast(bar) ///
file1opts(color(gs12)) ci1opts(color(black)) ///
labels("Overall" "Republicans" "Independents" "Democrats") ///
xtitle("") ytitle("Effect of Being a Woman on Pr(Turnout)") ///
ylab(-.03(.01)0.03, format(%9.2f)) ///
title("")

graph export "Effects of Gender in all 4 models percent women candidate.png", replace

combomarginsplot overall_womancandummy repub_womancandummy indp_womancandummy dem_womancandummy, ///
recast(bar) ///
file1opts(color(gs12)) ci1opts(color(black)) ///
labels("Overall" "Republicans" "Independents" "Democrats") ///
xtitle("") ytitle("Effect of Being a Woman on Pr(Turnout)") ///
ylab(-.03(.01)0.03, format(%9.2f)) ///
title("")

graph export "Effects of Gender in all 4 models woman in primary dummy.png", replace


*****Creating Tables******

label var percent_womancandidates "Percent Woman Candidates"

esttab overall_percentwomen repub_percentwomen indp_percentwomen dem_percentwomen using primary_percentwomen.csv, replace se(3) label  ///
		b(%9.2f) star(* .1 ** .05 *** .01) nolz nobase nogaps s(N ll r2_p bic) ///
		varlabels(age_bounded_100 "Age" age_bounded_100_sq "Age Squared" 1.female "Female" asian_fix "Asian" black_nh_fix "Black" hisp_fix "Latino" ///
		race_other_nh_fix "Race Other" married "Married" educ_prob_college "Pr(Bachelor's degree)" income "Income" ideology_continuous ///
		"Catalist Ideology" _cons "Constant" 1.open_unaff_primary "Open-Unaffl. Voters Primary State" 1.open_primary "Open Primary State" ///
		1.non_partisan_primary "Non-partisan Primary State" ///
		rfrac_overall "Rep. Fract. 2022" dfrac_overall "Dem. Fract. 2022" ///
		vote_2018p "Vote in 2018 Primary") ///
		mtitles("Overall" "Republicans" ///
		"Independents" "Democrats" ) ///
		order(1.female percent_womancandidates vote_2018p age_bounded_100  age_bounded_100_sq ///
		asian_fix black_nh_fix hisp_fix race_other_nh_fix married educ_prob_college income ///
		ideology_continuous 1.open_unaff_primary 1.open_primary 1.non_partisan_primary)

label var woman_in_primary "Woman in Primary"
esttab overall_womancandummy repub_womancandummy indp_womancandummy dem_womancandummy using primary_womancandummy.csv, replace se(3) label  ///
		b(%9.2f) star(* .1 ** .05 *** .01) nolz nobase nogaps s(N ll r2_p bic) ///
		varlabels(age_bounded_100 "Age" age_bounded_100_sq "Age Squared" 1.female "Female" asian_fix "Asian" black_nh_fix "Black" hisp_fix "Latino" ///
		race_other_nh_fix "Race Other" married "Married" educ_prob_college "Pr(Bachelor's degree)" income "Income" ideology_continuous ///
		"Catalist Ideology" _cons "Constant" 1.open_unaff_primary "Open-Unaffl. Voters Primary State" 1.open_primary "Open Primary State" ///
		1.non_partisan_primary "Non-partisan Primary State" ///
		rfrac_overall "Rep. Fract. 2022" dfrac_overall "Dem. Fract. 2022" ///
		vote_2018p "Vote in 2018 Primary") ///
		mtitles("Overall" "Republicans" ///
		"Independents" "Democrats" ) ///
		order(1.female 1.woman_in_primary vote_2018p age_bounded_100  age_bounded_100_sq ///
		asian_fix black_nh_fix hisp_fix race_other_nh_fix married educ_prob_college income ///
		ideology_continuous 1.open_unaff_primary 1.open_primary 1.non_partisan_primary)
